//
//  ViewController.m
//  ImageEdit
//
//  Created by ihope99 on 2017/7/18.
//  Copyright © 2017年 GYP. All rights reserved.
//

#import "SetIconViewController.h"

@interface SetIconViewController (){
    UIImage *mKeyImage;
    NSArray *sizeArray;
}

@end

@implementation SetIconViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton  *mbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    mbutton.backgroundColor = [UIColor redColor];
    mbutton.frame = CGRectMake(0, 0, 200, 200);
    mbutton.center = self.view.center;
    [self.view addSubview:mbutton];
    [mbutton addTarget:self action:@selector(mButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    mKeyImage = [UIImage imageNamed:@"set_icon"];
    
    
    sizeArray = @[@"40",@"60",@"58",@"87",@"80",@"120",@"120",@"180",@"1024"];
    
}
-(void)mButtonAction{
    for (NSInteger i = 0; i < sizeArray.count; i ++ ) {
        NSInteger m = [sizeArray[i] integerValue];
        CGSize size = CGSizeMake(m, m);
        UIImage *newImage = [self imageWithImageSimple:mKeyImage scaledToSize:size];
        NSString *mStr = [NSString stringWithFormat:@"icon_%lu",i];
        NSData *data = UIImagePNGRepresentation(newImage);
        [self writePath:mStr data:data];
    }
    
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Contents" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 最终路径
    jsonPath = [documentPath stringByAppendingString:@"/Contents.json"];
    BOOL isYes = [data writeToFile:jsonPath atomically:YES];
    if (isYes) {
        NSLog(@"文件写入成功");
        NSLog(@"%@",jsonPath);
    }
    else{
        NSLog(@"文件写入失败");
    }
    
}

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    
}

-(void)writePath:(NSString *)path data:(NSData *)data{

//    UIImage *image = [UIImage imageNamed:@"v_red_heart_selected@3x"];
//    // 将image类型的对象转换成NSData类型的数据进行存储
//    // 使用UIImageJPEGRepresentation将图片转换成NSData类型的
//    // 第一个参数：要转换的image对象
//    // 第二个参数：表示图片压缩的值compressionQuality
//    // iPhone中将大于2M的图片，会自动旋转90度压缩处理，所以最终会将图片保存成jpeg格式的
//    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    // 找到路径进行存储
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 最终路径
    NSString *imagePath = [documentPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",path]];
    
    BOOL isYes = [data writeToFile:imagePath atomically:YES];
    if (isYes) {
        NSLog(@"成功");
        NSLog(@"%@",imagePath);
    }
    else{
        NSLog(@"失败");
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
